package com.grm.async;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import com.grm.entity.Brand;

/**
 * desc: 模拟跨服务调用，根据不同类型获取不同结果，每一个类型都使用异步方法，最后再汇总结果
 *
 * @author gaorimao
 * @since 2021-3-14
 */
@Component
public class CallRemoteMethodAsyncTask {
	private static Logger logger = LoggerFactory.getLogger(CallRemoteMethodAsyncTask.class);
	
	@Async("callRemoteMethodExecutor")
    public Future<List<Brand>> queryPhones( ){
		logger.info("[CallRemoteMethodAsyncTask] queryPhones start...");
        List<Brand> phones=new ArrayList<>();
        //模拟数据库查询
        phones.add(new Brand(1L,"苹果11","phone",8699.00));
        phones.add(new Brand(2L,"华为p40 pro","phone",6899.00));
        phones.add(new Brand(3L,"小米11 pro","phone",4899.00));
        return new AsyncResult<List<Brand>>(phones);
    }
	
	@Async("callRemoteMethodExecutor")
    public Future<List<Brand>> queryComputers( ){
		logger.info("[CallRemoteMethodAsyncTask] queryComputers start...");
        List<Brand> computers=new ArrayList<>();
        //模拟数据库查询
        computers.add(new Brand(1L,"matebook14","computer",4699.00));
        computers.add(new Brand(2L,"mac","computer",13899.00));
        computers.add(new Brand(3L,"联想x","computer",8999.00));
        return new AsyncResult<List<Brand>>(computers);
    }
	
	@Async("callRemoteMethodExecutor")
    public Future<List<Brand>> queryPads( ){
		logger.info("[CallRemoteMethodAsyncTask] queryPads start...");
        List<Brand> pads=new ArrayList<>();
        //模拟数据库查询
        pads.add(new Brand(1L,"ipad 11 pro","pad",9699.00));
        pads.add(new Brand(2L,"三星 s7 ipad","pad",7899.00));
        pads.add(new Brand(3L,"华为ipad","pad",6899.00));
        return new AsyncResult<List<Brand>>(pads);
    }
}
